package com.atguigu.tingshu.order.pattern;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public abstract class AbstractOrderConfirmPolicy implements OrderConfirmPolicy {
    @Autowired
    private RedisTemplate redisTemplate;

    public void tradeNo(OrderInfoVo orderInfoVo,Long userId){
        //生成订单流水号
        String tradeKey = RedisConstant.ORDER_TRADE_NO_PREFIX + userId;
        String tradeNo = IdUtil.randomUUID();
        redisTemplate.opsForValue().set(tradeKey, tradeNo, 5, TimeUnit.MINUTES);
        orderInfoVo.setTradeNo(tradeNo);
    }

    public void tradeSign(OrderInfoVo orderInfoVo){
        //封装订单签名信息
        orderInfoVo.setTimestamp(System.currentTimeMillis());
        Map<String, Object> map = BeanUtil.beanToMap(orderInfoVo, false, true);
        //5.3.2 调用生成签名工具方法，底层就是Md5,对订单VO中所有属性值进行排序，拼接成字符串+秘钥，进行MD5加密，返回签名值
        String sign = SignHelper.getSign(map);
        orderInfoVo.setSign(sign);
    }
}
